<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>定时任务-主页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索条件(<b style="color: #1E9FFF;cursor: pointer">收起--展开</b>)</legend>
            <form class="layui-form layui-form-pane" lay-filter="search-form" id="search-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">任务ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="quartzId" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">任务名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="quartzName" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">任务类型</label>
                        <div class="layui-input-inline">
                            <select name="quartzType">
                                <option value="">请选择</option>
                                <option value="0">内置</option>
                                <option value="1">外配</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">启停状态</label>
                        <div class="layui-input-inline">
                            <select name="quartzStatus">
                                <option value="">请选择</option>
                                <option value="0">启动</option>
                                <option value="1">停止</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <input id="startDate" name="startDate" type="hidden" value=""/>
                        <input id="endDate" name="endDate" type="hidden" value=""/>
                        <label class="layui-form-label">执行时间</label>
                        <div class="layui-input-inline" style="width: 180px;">
                            <input type="text" autocomplete="off" id="execTimeRange" class="layui-input" placeholder="开始日期~结束时间">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-bg-blue"  lay-submit lay-filter="search-btn"><i class="layui-icon layui-icon-search"></i> 搜 索</button>
                        <button type="reset" class="layui-btn layui-btn-primary"  lay-submit lay-filter="reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置</button>
                    </div>
                </div>
            </form>
        </fieldset>
        <!-- 表格头部左侧工具栏 -->
        <script type="text/html" id="toolbar">
            <div class="layui-btn-group layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
            </div>
        </script>
        <!-- 数据表格 -->
        <table class="layui-hide" id="quartzList" lay-filter="quartzListFilter"></table>
        <!-- 表格行操作列 -->
        <script type="text/html" id="rowsBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon  layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-orange " lay-event="detail"><i class="layui-icon layui-icon-fire"></i>日志</a>
        </script>
    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script th:src="@{/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/js/table-form.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['form', 'laydate','table','notice'], function () {
        let $ = layui.jquery,
            form = layui.form,
            notice = layui.notice,
            laydate = layui.laydate,
            table = layui.table;

        let ctxPath = /*[[@{/}]]*/'';

        //渲染时间
        laydate.render({
            elem: '#execTimeRange',
            type: 'date',
            range: '~',
            format: 'yyyy/MM/dd',
            done: function(value, date1, date2){
                $('#startDate').val(startDateTimeFormat(date1));
                $('#endDate').val(endDateTimeFormat(date2));
            }
        });

        //渲染表格
        let tableIns = table.render({
            elem: '#quartzList',
            url: ctxPath+'system/sysQuartz/quartzList',
            title:'参数信息',
            height: 'full-100',
            method : 'GET',
            toolbar: '#toolbar',                                //绑定表格头部左侧工具栏
            defaultToolbar: ['filter', 'exports', 'print', {    //开启默认表格头部右侧图标
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: 'radio', width: 30,align:'center'},
                    {field: 'quartzId', width: 180, title: '任务ID',align:'center'},
                    {field: 'quartzName', width: 200, title: '任务名',align:'left',sort:true},
                    {field: 'quartzDesc', width: 200, title: '任务描述',align:'center'},
                    {field: 'quartzCron', width: 120, title: 'Cron表达式',align:'center'},
                    {field: 'quartzCronZh', width: 180, title: 'Cron中文含义',align:'center'},
                    {field: 'quartzClass', width: 300, title: '执行方法',align:'center',hide: true},
                    {field: 'quartzParam', width: 300, title: '执行参数',align:'center',hide: true},
                    {field: 'quartzType', width: 110, title: '任务类型',align:'center',sort:true, hide: true,templet:function(d){
                        return (0 === d.quartzType)? '内置' : '外配'
                    }},
                    {field: 'quartzStatus', width: 110, title: '启停状态',align:'center', templet:function(d){
                        if(d.quartzStatus == 0){
                            return "<input type='checkbox' value='"+d.quartzId+"' title='ON|OFF' lay-skin='switch' lay-filter='quartzStatusFilter' checked='checked'";
                        }else{
                            return "<input type='checkbox' value='"+d.quartzId+"' title='ON|OFF' lay-skin='switch' lay-filter='quartzStatusFilter'";
                        }
                    }},
                    {field: 'lastExecStatus', width: 110, title: '执行状态',align:'center',sort:true, templet:function(d){
                            return (0 === d.lastExecStatus) ? '执行成功' : '执行失败'
                    }},
                    {field: 'lastExecTime', width: 180, title: '执行时间',align:'center'},
                    {field: 'nextExecTime', width: 180, title: '下次执行时间',align:'center',sort:true},
                    {field: 'createTime', width: 180, title: '创建时间',align:'center',sort:true, hide: true},
                    {title: '操作', width: 220, toolbar: '#rowsBar', align: 'center',fixed: 'right'}
                ]
            ],
            page: true,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15
        });

        //启停状态
        form.on('switch(quartzStatusFilter)', function(obj){
            updateQuartzStatus(this.value,obj.elem.checked);
        });

        //监听查询条件提交按钮
        form.on('submit(search-btn)', function (data) {
            //执行搜索重载
            table.reload('quartzList', {
                page: {curr: 1},
                where: form.val('search-form')
            }, 'data');
            return false;
        });

        //监听表格头部工具栏事件
        table.on('toolbar(quartzListFilter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddOrUpdateQuartzDialog();
                    break;
                case 'delete':
                    let checkStatus = table.checkStatus(obj.config.id);
                    let quartzIds = "";
                    let delFlag = false;
                    $.each(checkStatus.data,function (k,v){
                        quartzIds = quartzIds + ","+v.quartzId;
                        //内置定时任务不允许删除
                        if(v.quartzType === 0){
                            delFlag = true;
                        }
                    })

                    dealQuartz(quartzIds,delFlag);
                    break;
            };
        });

        //监听表格行操作列事件
        table.on('tool(quartzListFilter)', function (obj) {
            switch (obj.event) {
                case 'edit':
                    openAddOrUpdateQuartzDialog(obj.data.quartzId);
                    break;
                case 'detail':
                    openQuartzLogDialog(obj.data.quartzId,obj.data.quartzName);
                    break;
                case 'delete':
                    //内置定时任务不允许删除
                    if(obj.data.paramType === 0){
                        dealQuartz(obj.data.quartzId,true);
                    }else {
                        dealQuartz(obj.data.quartzId,false);
                    }
                    break;
            };
        });

        //打开添加或修改参数页面
        function openAddOrUpdateQuartzDialog(quartzId) {
            let url = ctxPath+'system/sysQuartz/quartzAddOrUpdate';
            let title = '<i class="layui-icon layui-icon-util">新增定时任务</i>';
            if(quartzId){
                url = url+"?quartzId="+encodeURIComponent(quartzId);
                title = '<i class="layui-icon layui-icon-util">更新定时任务</i>';
            }
            layer.open({
                title: title,
                type: 2,
                shade: 0.2,
                shadeClose: true,
                offset:'r',
                area: ['40%', '100%'],
                content: url
            });
        }

        //打开日志页面
        function openQuartzLogDialog(quartzId,quartzName) {
            layer.open({
                title: '['+quartzName+']执行日志',
                type: 2,
                shade: 0.2,
                shadeClose: true,
                area: ['80%', '90%'],
                content: ctxPath+'system/sysQuartzLog/quartzLogMain?quartzId='+encodeURIComponent(quartzId)
            });
        }

        //批量删除参数
        function dealQuartz(datas,delFlag) {
            if(delFlag){
                notice.warning('不允许删除内置定时任务！');
                return;
            }
            if(datas.length === 0){
                notice.warning('请选择需要删除的记录！');
                return;
            }
            layer.confirm('您确定要删除选中的记录吗？',{title:'提示',icon:0,btn:['确定','取消']},function (index) {
                layer.close(index);
                $.ajax({
                    type:'DELETE',
                    url:ctxPath+'system/sysQuartz/deleteQuartz/'+datas,
                    dataType:'json',
                    success:function (res){
                        if(res.code === 200){
                            notice.success('删除成功',function(){
                                tableIns.reload();
                            });
                        }else{
                            notice.error(res.msg);
                        }
                    }
                });
            })
        }

        //更新任务状态
        function updateQuartzStatus(quartzId,flag) {
            if(null === quartzId){
                notice.warning('请选择需要更新的记录！');
                return;
            }
            let statusDesc = flag ? '启动' : '停止';
            layer.confirm('您确定要'+statusDesc+'选中的记录？',{title:'提示',icon:0,btn:['确定','取消']},function (index) {
                layer.close(index);
                $.ajax({
                    type:'PUT',
                    url:ctxPath+'system/sysQuartz/updateQuartzStatus/',
                    data:{'quartzId':quartzId,'quartzStatus':flag ? 0 : 1},
                    dataType:'json',
                    success:function (res){
                        if(res.code === 200){
                            notice.success('更新成功',function(){
                                tableIns.reload();
                            });
                        }else{
                            notice.error(res.msg);
                        }
                    }
                });
            })
        }
    });
</script>
</body>
</html>